# Connection Dialog module for CROSS

#########################################################################################
#
# Copyright (C) 2008  
# Authors: Aljosa Osep (aljosa.osep@gmail.com), Jernej Kranjec (jernej.kranjec@gmail.com)
# From Faculty of Electrical Engineering and Computer Science, Maribor, Slovenia
# Done at School of Technology, Seinajoki, Finland
# Under mentor Kimmo Salmenjoki (kimmo.salmenjoki@seamk.fi)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################################

# ==================== imports ====================== #
import sys
sys.path.append('Modules/GUI/Widgets')
sys.path.append('Modules/Core')

import pygtk            # import pygtk library
pygtk.require('2.0')    # require version 2.o
import gtk 	        # import gtk

import tabWidgetsModule # custom widgets used here	
import exceptionModule	# import our custom exceptions module

############################
# connection dialog class
############################
class connectionDialog(gtk.Dialog):
	# methods
	def message_box(self, message):
		self.msg_dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, message)

		self.msg_dialog.run()
		self.msg_dialog.destroy()

	#initialisation
	def __init__(self, communicator):
		self.communicator = communicator			# set pointer to communicator

		gtk.Dialog.__init__(self, "Connection settings", buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_APPLY, gtk.RESPONSE_APPLY))
		self.set_resizable(False)				# fixed window
		self.set_size_request(300, 450)				# size 300x450px

		self.conn_frame = tabWidgetsModule.custom_frame_widget("Connection")		# create read frame
		self.vbox.pack_start(self.conn_frame.get_frame(), True, True, 9)		# pack read frame

		# append widgets to frame
		self.conn_frame.append_widget(tabWidgetsModule.singleLine("Port", True, str(communicator.port)))
		self.conn_frame.append_widget(tabWidgetsModule.singleLine("Timeout", True, str(communicator.timeout)))
		self.conn_frame.append_widget(tabWidgetsModule.singleLine("XonfOff", True, str(communicator.xonoff)))
		self.conn_frame.append_widget(tabWidgetsModule.singleLine("RtsCts", True, str(communicator.rtscts)))
		self.conn_frame.append_widget(tabWidgetsModule.singleLine("Baudrate", True, str(communicator.baudrate)))

		self.conn_frame.append_widget(tabWidgetsModule.connCombo("Parity", ("none", "even       ", "odd"), True, communicator.serialSettingToValueParity()-1))
		self.conn_frame.append_widget(tabWidgetsModule.connCombo("Bytesize", ("fivebits", "sixbits", "sevenbits", "eightbits" ), True, communicator.serialSettingToValueBytesize()-1))
		self.conn_frame.append_widget(tabWidgetsModule.connCombo("Stopbits", ("one", "two         "), True, communicator.serialSettingToValueStopbits()-1))
		
		self.conn_frame.pack_controls()				# pack appended widgets to frame

		response = self.run()					# get response from dialog

		if response == gtk.RESPONSE_APPLY:			# apply connection settings				
			communicator.configureToFile(self.conn_frame.controls[0].get_entry_text(), self.conn_frame.controls[1].get_entry_text(),
			self.conn_frame.controls[2].get_entry_text(), self.conn_frame.controls[3].get_entry_text(), 
			self.conn_frame.controls[4].get_entry_text(), self.conn_frame.controls[5].get_active_index()+1, 
			self.conn_frame.controls[6].get_active_index()+1, self.conn_frame.controls[7].get_active_index()+1)

			try:
				self.communicator.open_connection(reopen=True, config=True)
			except exceptionModule.SerialPortException, e:				# if comes to SerialPortException, port openning failed
				self.message_box("Error opening serial port! Probably configuration problems, please, try diffrent settings. Trying with default values...")
				try:
					self.communicator.open_connection(reopen=True, config=False)
				except exceptionModule.SerialPortException, e:				
					self.message_box("Still doesn't work.")	
	#	elif response == gtk.RESPONSE_CANCEL:			# if acton is cancelled, just pass to self destruction
	#		pass

		self.destroy()
		self.ret = None




		
